package com.zonski.jbots.engine;

/**
 * Simple rectangle class
 */
public final class Rectangle
{
    /**
     * Expands the specified rectangle by the specified
     * x and y values, if x or y are negative then the
     * rectangle expands along the negative axis (ie. it
     * does not shrink)
     */
    public static final void expand(Rectangle r, int x, int y, Rectangle toFill)
    {
        int rx;
        int ry;
        int rw;
        int rh;
        if(x >= 0)
        {
            rx = r.x;
            rw = r.width + x;
        }else{
            rx = r.x + x;
            rw = r.width - x;
        }
        if(y >= 0)
        {
            ry = r.y;
            rh = r.height + y;
        }else{
            ry = r.y + y;
            rh = r.height - y;
        }
        toFill.setLocation(rx, ry, rw, rh);
    }
    
    public int x;
    public int y;
    public int width;
    public int height;

    public int rightEdge;
    public int bottomEdge;

    public Rectangle(int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.rightEdge = x + width;
        this.bottomEdge = y + height;
    }

    public final void setLocation(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.rightEdge = this.x + this.width;
        this.bottomEdge = this.y + this.height;
    }

    public final void setLocation(int x, int y, int width, int height)
    {
        this.width = width;
        this.height = height;
        setLocation(x, y);
    }

    public final void setDimensions(int width, int height)
    {
        this.width = width;
        this.height = height;
        this.rightEdge = this.x + this.width;
        this.bottomEdge = this.y + this.height;
    }

    public final void setX(int x)
    {
        this.x = x;
        this.rightEdge = this.x + this.width;
    }

    public final void setX(int x, int width)
    {
        this.width = width;
        this.setX(x);
    }

    public final void setWidth(int width)
    {
        this.width = width;
        this.rightEdge = this.x + this.width;
    }

    public final void setY(int y)
    {
        this.y = y;
        this.bottomEdge = this.y + this.height;
    }

    public final void setY(int y, int height)
    {
        this.height = height;
        this.setY(y);
    }

    public final void setHeight(int height)
    {
        this.height = height;
        this.bottomEdge = this.y + this.height;
    }

    public final boolean overlaps(Rectangle r)
    {
        return
            this.x < r.rightEdge
            &&
            r.x < this.rightEdge
            &&
            this.y < r.bottomEdge
            &&
            r.y < this.bottomEdge;
    }

    public final boolean contains(Rectangle r)
    {
        return this.x <= r.x
                &&
                this.rightEdge >= r.rightEdge
                &&
                this.y <= r.y
                &&
                this.bottomEdge >= r.bottomEdge;
    }

    public final boolean contains(int x, int y)
    {
        return x >= this.x && y >= this.y && x < this.rightEdge && y < this.bottomEdge;
    }

    /**
     * Centres this rectangle in the specified rectangle
     */
    public final void setCentre(Rectangle r)
    {
        setLocation(r.x + (r.width - this.width)/2, r.y + (r.height - this.height)/2);
    }

    public final void setCentre(int cx, int cy)
    {
        setLocation(cx - this.width/2, cy - this.height/2);
    }

    public final void setCentre(int cx, int cy , int width, int height)
    {
        this.width = width;
        this.height = height;
        this.setCentre(cx, cy);
    }

    public final int getCentreX()
    {
        return this.x + this.width/2;
    }

    public final int getCentreY()
    {
        return this.y + this.height/2;
    }

    public boolean equals(Object o)
    {
        // TODO : a faulty (but quick) implementation of equals
        return equals((Rectangle)o);
    }

    public boolean equals(Rectangle r)
    {
        boolean b;
        if(r != null)
        {
            b = r.x == this.x && r.y == this.y &&
                    r.width == this.width && r.height == this.height;
        }else{
            b = false;
        }
        return b;
    }

    public String toString()
    {
        return "{"+this.x+","+this.y+","+this.width+","+this.height+"}";
    }

}
